package com.sskj.phoenixcoin.activity;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.phoenixcoin.R;
import com.sskj.phoenixcoin.bean.AppBank;
import com.sskj.phoenixcoin.component.DaggerUserDataComponent;
import com.sskj.phoenixcoin.presenter.AppAddBankActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;


public class AppAddBankActivity extends BaseActivity<AppAddBankActivityPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.bank_person_name)
    EditText bankPersonName;
    @BindView(R.id.first_line_show)
    View firstLineShow;
    @BindView(R.id.bank_num)
    EditText bankNum;
    @BindView(R.id.second_line_show)
    View secondLineShow;
    @BindView(R.id.bank_name)
    EditText bankName;
    @BindView(R.id.bank_branch_edit)
    EditText bankBranchEdit;
    @BindView(R.id.four_line_show)
    View fourLineShow;
    @BindView(R.id.bank_add_btn)
    Button bankAddBtn;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private boolean isUpdate;  // 编辑银行卡
    private AppBank appBank;  // 银行卡id

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_add_bank;
    }

    @Override
    public AppAddBankActivityPresenter getPresenter() {
        return new AppAddBankActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        appBank = (AppBank) getIntent().getSerializableExtra("appBank");
        isUpdate = getIntent().getBooleanExtra("flag", false);
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
        tvTitle.setText(isUpdate ? "编辑银行卡" : "添加银行卡");
        bankAddBtn.setText(isUpdate ? "确定" : "添加");
        if (appBank != null) {
            bankPersonName.setText(appBank.getUsername());
            bankName.setText(appBank.getBankCardType());
            bankBranchEdit.setText(appBank.getBankCardOpenBank());
            bankNum.setText(appBank.getBankCardNo());
        }
        ClickUtil.click(ivBack, () -> {
            finish();
        });
        ClickUtil.click(bankAddBtn, () -> {
            String personName = bankPersonName.getText().toString().trim();
            String bankNameEdit = bankName.getText().toString().trim();
            String bankBranch = bankBranchEdit.getText().toString().trim();
            String bankNumEdit = bankNum.getText().toString().trim();
            if (TextUtils.isEmpty(personName)) {
                ToastUtil.showShort("请输入您的姓名");
                return;
            }
            if (TextUtils.isEmpty(bankNameEdit)) {
                ToastUtil.showShort("请输入您的开户行");
                return;
            }
            if (TextUtils.isEmpty(bankBranch)) {
                ToastUtil.showShort("请输入您的银行分行");
                return;
            }
            if (TextUtils.isEmpty(bankNumEdit)) {
                ToastUtil.showShort("请输入您的银行卡号");
                return;
            }
            mPresenter.updateBank(String.valueOf(userData.getId()), appBank != null ? appBank.getId() : "", bankNumEdit, bankNameEdit, bankBranch, personName);
        });
    }

    public static void start(Context context, boolean flag, AppBank appBank) {
        Intent intent = new Intent(context, AppAddBankActivity.class);
        intent.putExtra("flag", flag);
        intent.putExtra("appBank", appBank);
        context.startActivity(intent);
    }
}
